//人员管理功能相关配置
import axios from "axios";
import { nanoid } from "nanoid";

const personOptions = {
    namespaced: true,
    //actions用于响应组件的动作
    actions: {
        addPersonWang(context, value) {
            if (value.name.indexOf("王") === 0) {
                context.commit("ADD_PERSON", value);
            } else {
                alert("必须姓王")
            }
        },
        addPersonServer(context) {
            axios.get("https://api.uixsj.cn/hitokoto/get?type=social").then(
                response => {
                    console.log(response.data);
                    context.commit("ADD_PERSON", { id: nanoid(), name: response.data });
                },
                error => {
                    alert(error.message);
                }
            )
        }
    },

    //mutations用于操作数据(state)
    mutations: {
        ADD_PERSON(state, value) {
            state.personList.unshift(value);
        }
    },

    //state用于存储数据
    state: {
        personList: [
            {
                id: "001",
                name: "张三",
            },
        ],
    },

    //getters用于将state中的数据进行加工
    getters: {
        firstPersonName(state) {
            return state.personList[0].name;
        }
    },
}

export default personOptions;
